<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use yii\helpers\ArrayHelper;
use common\models\fenhuiartic\Fenhuiartic;
use common\models\fenhuicat\Fenhuicat;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel common\models\fenhuiartic\FenhuiarticSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', '分会文章管理');
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $this->beginBlock('siderbar'); ?>
<?= $this->render('//layouts/fenhui-menu') ?>
<?php $this->endBlock(); ?>
<div class="fenhuiartic-index">


    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
    <p>
        <?= Html::a(Yii::t('app', 'Create'), ['create'], ['class' => 'btn btn-success']) ?>
        <button class="btn btn-warning" id="stop" type="button">停用</button>
        <button class="btn btn-primary" id="start" type="button">启用</button>
        <button class="btn btn-danger" id="alldel" type="button">删除</button>
        <button class="btn btn-danger" id="move" data-toggle="modal" data-target="#myModal" type="button">批量移动文章</button>
    </p>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
    <?= GridView::widget([
        'id' => "myform",
        'pjax'=>false,
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'containerOptions'=>['style'=>'overflow: auto'], // only set when $responsive = false
        'headerRowOptions'=>['class'=>'kartik-sheet-style'],
        'filterRowOptions'=>['class'=>'filters'],
        'panel'=>[
            'heading'=>'<h3 class="panel-title"><i class="glyphicon glyphicon-globe"></i> 数据列表</h3>',
            'type'=>'default',
            'footer'=>false
        ],
        'toolbar' => ['{export}', '{toggleData}'],
        'pager'=>[
            'options'=>['class'=>'hidden']//关闭自带分页
        ],
        'persistResize'=>false,

        'columns' => [
            [
                'class'=>'kartik\grid\CheckboxColumn',
                'headerOptions'=>['class'=>'my-table'],
            ],

            'id',
            'title',
            [
                'headerOptions' => ['width' => '180'],
                'attribute'=>'fc_id',
                'value'=>function ($model) {
                    if(isset($model->fenhuicat)){
                        $name =  $model->fenhuicat->name;
                    }else{
                        $name= "无";
                    }
                    return  $name;
                },
                'filterType'=>GridView::FILTER_SELECT2,
                'filter'=>Fenhuiartic::getArrayFenhuicat(),
                'filterWidgetOptions'=>[
                    'pluginOptions'=>['allowClear'=>true],
                ],
                'filterInputOptions'=>['placeholder'=>'全部'],
                'format'=>'raw'

            ],
            [
                'headerOptions' => ['width' => '180'],
                'attribute'=>'fg_id',
                'value'=>function ($model) {
                    if(isset($model->fenhuigrade)){
                        $name =  $model->fenhuigrade->name;
                    }else{
                        $name= "无";
                    }
                    return  $name;
                },
                'filterType'=>GridView::FILTER_SELECT2,
                'filter'=>Fenhuiartic::getArrayFenhuigrade(),
                'filterWidgetOptions'=>[
                    'pluginOptions'=>['allowClear'=>true],
                ],
                'filterInputOptions'=>['placeholder'=>'全部'],
                'format'=>'raw'

            ],

            'subhead',
            [
                'attribute'=>'intro',
                'value'=> function ($model){
                    return mb_substr($model->intro,0,30,'utf-8');
                },
            ],

            [
                'attribute' => 'status',
                'format' => 'html',
                'value' => function ($model) {
                    if ($model->status === $model::STATUS_ACTIVE) {
                        $class = 'label-success';
                    } elseif ($model->status === $model::STATUS_DELETED) {
                        $class = 'label-warning';
                    } else {
                        $class = 'label-danger';
                    }

                    return '<span class="label ' . $class . '">' . $model->statusLabel . '</span>';
                },
                'filterType'=>GridView::FILTER_SELECT2,
                'filter'=>Fenhuiartic::getArrayStatus(),
                'filterWidgetOptions'=>[
                    'pluginOptions'=>['allowClear'=>true],
                ],
                'filterInputOptions'=>['placeholder'=>'全部'],
                'format'=>'raw'

            ],

            [
                'class' => 'yii\grid\ActionColumn',
                'header' => '操作',
                'template' => '{view}{update}{delete}',
                'headerOptions' => ['width' => '200'],
                'buttons' => [
                    'view' => function ($url, $model, $key) {
                        return  Html::a('&nbsp;&nbsp;<span class="glyphicon glyphicon-eye-open" ><span class="classformat">查看</span></span>', $url, ['title' => '查看'] ) ;
                    },
                    'update' => function ($url, $model, $key) {
                        return Html::a('&nbsp;&nbsp;<span class="glyphicon glyphicon-pencil"><span class="classformat">修改</span></span>', $url, ['title' => '修改'] ) ;
                    },
                    'delete' => function ($url, $model, $key) {
                        return Html::a('&nbsp;&nbsp;<span class="glyphicon glyphicon-trash" ><span class="classformat">删除</span></span>', $url, [
                            'title' => '删除',
                            'data'=>[
                                'confirm'=>'你确定要删除'.$model->title.'吗？',
                                'method'=>'post'
                            ]
                        ] ) ;
                    },

                ],
            ],
        ],
    ]); ?>
    <?= \backend\components\GoLinkPager::widget([
        'pagination' => $dataProvider->pagination,
        'go' => true,
    ]); ?>

</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">文章分类列表</h4>
            </div>
            <div class="modal-body">

                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title">选择文章分类</h3>
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body no-padding">
                        <table class="table table-condensed" >
                            <tbody id="selcttable">



                            </tbody>
                        </table>
                    </div>
                    <!-- /.box-body -->
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="savemodal">保存</button>
            </div>
        </div>
    </div>
</div>

<?php $this->beginBlock('footer_scripts'); ?>
<script>
    $(document).ready(function(){

        $('#myModal').on('show.bs.modal', function (event) {

            var checkboxid = "";
            var selname = '';
            var inputhtml = '';
            $('input:checkbox[name="selection[]"]:checked').each(function (i) {
                if (0 == i) {
                    selname = $(this).parent().siblings().html();
                    checkboxid = $(this).val();
                } else {
                    selname += ("," + $(this).parent().siblings().html());
                    checkboxid += ("," + $(this).val());
                }
            });

            //var button = $(event.relatedTarget) // Button that triggered the modal
            //var recipient = button.data('whatever') // Extract info from data-* attributes
            // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
            // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
            var url = "<?php echo Url::to(['fenhuiartic/catelist']);?>";
            $.post(url,{'id': checkboxid,'_csrf':'<?php echo yii::$app->request->getCsrfToken();?>'},function (data){

                var objjson = $.parseJSON(data);
                var inputhtml = "<tr><th style='width: 10px'>#</th><th>文章分类</th></tr><input type='hidden' id='articid' name='articid' value='"+checkboxid+"' />";
                $.each(objjson.cate, function(name, value) {


                    inputhtml+="<tr><td><input type='radio' name='isselect'  value='"+value.id+"'/></td><td>"+value.name+"</td></tr> ";


                });
                // console.log(inputhtml);
                $('#selcttable').html(inputhtml);
            }).error(function() { alert("没有权限")  });
        });


        $('#savemodal').on('click',function(){
            var id =  $('input:radio:checked').val();
            var url = "<?php echo Url::to(['fenhuiartic/changecate']);?>";
            var articid = $('#articid').val();
            if(articid == "") {
                alert("你没有选择移动项！！！");
                return (location.reload());
            }

            $.post(url,{'id':id,'articid':articid,'_csrf':'<?php echo yii::$app->request->getCsrfToken();?>'},function (data){

                alert("修改成功");
                location.reload();
            }).error(function() { alert("没有权限,请联系管理员")  });
        });

        bindcheck("<?php echo Url::to(['fenhuiartic/start']);?>");
        binduncheck("<?php echo Url::to(['fenhuiartic/stop']);?>");
        binddel("<?php echo Url::to(['fenhuiartic/alldelete']);?>");
    });
</script>
<?php $this->endBlock(); ?>

